package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequestPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestPermission(name = "员工列表",expression = "employee.list")
    @RequestMapping("/list")
    private String list(Model model, EmployeeQueryObject qo){
        model.addAttribute("qo",qo);
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("departments",departmentService.selectAll());
        return "employee/list";
    }

    @RequestPermission(name = "员工删除",expression = "employee.delete")
    @RequestMapping("/delete")
    private String delete(Long id) throws InterruptedException {
        employeeService.delete(id);
        Thread.sleep(1000);
        return "redirect:/employee/list";
    }

    @RequestPermission(name = "员工编辑页面",expression = "employee.input")
    @RequestMapping("/input")
    private String input(Long id,Model model){
        //回显数据
        model.addAttribute("ro",employeeService.selectRolesByEmpId(id));
        model.addAttribute("emp",employeeService.selectId(id));
        model.addAttribute("roles",roleService.selectAll());
        model.addAttribute("departments",departmentService.selectAll());
        if (id != null){
            model.addAttribute("employee",employeeService.selectId(id));
        }
        return "employee/input";
    }

    @RequestPermission(name = "员工保存或修改",expression = "employee.saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    private String saveOrUpdate(Employee  employee, Long[] ids){
        if (employee.getId() != null){
            employeeService.update(employee,ids);
        }else {
            employeeService.insert(employee,ids);
        }
        return "redirect:/employee/list";
    }

    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        String fileName = new String("员工通讯录.xls".getBytes("UTF-8"), "ISO-8859-1");
        response.setHeader("Content-Disposition","attachment;filename=ABC.xls");
        employeeService.exportXls(response.getOutputStream());
    }

    @RequestMapping("importXls")
    public String importXls(MultipartFile file) throws IOException {

        employeeService.importXls(file);
        return "redirect:/employee/list";

    }



}
